/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

package demo;

import html.HtmlAdapter;
import html.HtmlTreeNode;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import swing.HtmlTree;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;


public class demotree extends JPanel 
                              implements ActionListener {
    HtmlTree tree;
    private static String SHOW_ANCESTOR_CMD = "showAncestor";

    public demotree() {
        super(new BorderLayout());
        
        //Construct the panel with the toggle buttons.
        //Construct the tree.
        
		String html="<li><a href=\"/phap-luat-m12i349/\">Pháp luật</a></li>";
		Document doc = null;
		try {
			doc = Jsoup.connect("http://www.vinabook.com/")
					  .data("query", "Java")
					  .userAgent("Mozilla")
					  .cookie("auth", "token")
					  .timeout(3000)
					  .post();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String html1 = "<html><head><title>First parse</title></head>"
				  + "<body><p>Parsed HTML into a doc.</p></body></html>";
		HtmlAdapter adapter= new HtmlAdapter(doc.html());
		
		HtmlTreeNode root=adapter.getRoot();
		
		HtmlTreeNode root2= getGenealogyGraph();
        tree = new HtmlTree(root);
        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setPreferredSize(new Dimension(1000, 120));

        //Add everything to this panel.
        add(scrollPane, BorderLayout.CENTER);
    }

    /** 
     * Required by the ActionListener interface.
     * Handle events on the showDescendant and
     * showAncestore buttons. 
     */
    public void actionPerformed(ActionEvent ae) {
        if (ae.getActionCommand() == SHOW_ANCESTOR_CMD) {
            //tree.showAncestor(true);
        } else {
           // tree.showAncestor(false);
        }
    }
    
    /**
     *  Constructs the genealogy graph used by the model.
     */
    public HtmlTreeNode getGenealogyGraph() {
        //the greatgrandparent generation
    	
    	
        HtmlTreeNode a1 = new HtmlTreeNode("Jack (great-granddaddy)");
        HtmlTreeNode a2 = new HtmlTreeNode("Jean (great-granny)");
        HtmlTreeNode a3 = new HtmlTreeNode("Albert (great-granddaddy)");
        HtmlTreeNode a4 = new HtmlTreeNode("Rae (great-granny)");
        HtmlTreeNode a5 = new HtmlTreeNode("Paul (great-granddaddy)");
        HtmlTreeNode a6 = new HtmlTreeNode("Josie (great-granny)");

        //the grandparent generation
        HtmlTreeNode b1 = new HtmlTreeNode("Peter (grandpa)");
        HtmlTreeNode b2 = new HtmlTreeNode("Zoe (grandma)");
        HtmlTreeNode b3 = new HtmlTreeNode("Simon (grandpa)");
        HtmlTreeNode b4 = new HtmlTreeNode("James (grandpa)");
        HtmlTreeNode b5 = new HtmlTreeNode("Bertha (grandma)");
        HtmlTreeNode b6 = new HtmlTreeNode("Veronica (grandma)");
        HtmlTreeNode b7 = new HtmlTreeNode("Anne (grandma)");
        HtmlTreeNode b8 = new HtmlTreeNode("Renee (grandma)");
        HtmlTreeNode b9 = new HtmlTreeNode("Joseph (grandpa)");

        //the parent generation
        HtmlTreeNode c1 = new HtmlTreeNode("Isabelle (mom)");
        HtmlTreeNode c2 = new HtmlTreeNode("Frank (dad)");
        HtmlTreeNode c3 = new HtmlTreeNode("Louis (dad)");
        HtmlTreeNode c4 = new HtmlTreeNode("Laurence (dad)");
        HtmlTreeNode c5 = new HtmlTreeNode("Valerie (mom)");
        HtmlTreeNode c6 = new HtmlTreeNode("Marie (mom)");
        HtmlTreeNode c7 = new HtmlTreeNode("Helen (mom)");
        HtmlTreeNode c8 = new HtmlTreeNode("Mark (dad)");
        HtmlTreeNode c9 = new HtmlTreeNode("Oliver (dad)");

        //the youngest generation
        HtmlTreeNode d1 = new HtmlTreeNode("Clement (boy)");
        HtmlTreeNode d2 = new HtmlTreeNode("Colin (boy)");

        HtmlTreeNode.addChild(a1,b1);
        HtmlTreeNode.addChild(a1,b2);
        HtmlTreeNode.addChild(a1,b3);
       /* HtmlTreeNode.addChildren(a3,new HtmlTreeNode[] {b5,b6,b7});
        HtmlTreeNode.addChildren(a5,new HtmlTreeNode[] {b8,b9});
        HtmlTreeNode.addChildren(b3,new HtmlTreeNode[] {c1,c2,c3});
        HtmlTreeNode.addChildren(b4,new HtmlTreeNode[] {c4,c5,c6});
        HtmlTreeNode.addChildren(b8,new HtmlTreeNode[] {c7,c8,c9});
        HtmlTreeNode.addChildren(c4,new HtmlTreeNode[] {d1,d2});*/

        return a1;
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("GenealogyExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        demotree newContentPane = new demotree();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}